<?php
$access_level = 1;
$page_title = 'eTAT: Home';
require_once 'controller.php';
if(isset($params->mod_course)) {
	$course = new Course($params->mod_course);
	die(json_encode($course));
}
if(isset($_POST['add_course_submit'])) {
	$data = (object) array('title'=>$_POST['title'],'term'=>"{$_POST['term']}",'description'=>$_POST['description']);
	$id = $instructor->addCourse($data);
	redirect("course.php?c=$id");
}
if(isset($_POST['modify_course_submit'])) {
	$course = new Course($_POST['course_id_hidden']);
	$course->title = $_POST['title'];
	$course->term = $_POST['term'];
	$course->description = $_POST['description'];
	$course->updateCourse();
}
if($params->dc) {
	$instructor->removeCourse($params->dc);
	redirect('myetat.php');
}
require_once 'header.php';
?>
<div id="mainbody">
<div class="bodycontent">
	<div class="titletxt">My eTAT</div>
	<?php
	$courses = $instructor->getAllCourses();
	$count = 0;
	if($courses != null) {
		foreach($courses as $c) {
			$groups = $c->getAllGroups();
			$count++;
		}
		print '<br/>';
	} else {
	?>
	<p>Looks like you haven't added any courses for monitoring, yet.</p>
	<p>Let's get started by pressing the <b>Add Course</b> button below:</p>
	<?php
	}
	$form = new Form('add_course_form','post');
	$form->popupHeader("Add New Course",array('class'=>'popuphead centered'));
	$form->textInput('course_id_hidden','',array('class'=>'invisible'));
	$form->textInput('title','Course Title',array('style'=>'width:300px;'),true);
	$form->textInput('term','Course Term',array('style'=>'width:300px;'),false);
	$form->textArea('description','Description (optional)',4,36);
	$form->button('add_course_submit','Create',array('type'=>'submit','class'=>'right'));
	$form->button('modify_course_submit','Modify',array('type'=>'submit','class'=>'right','class'=>'invisible'));
	print popup('add-course',$form->build(),350,array('left'=>300,'top'=>(120+$count*15)));
	?>
	<table width="90%" border="0">
		<thead>
			<tr><td align="right" colspan="5"><a href="javascript:void(0);"
				onClick="javascript:popup('add-course',null);$('row-add-course-submit').className = 'right';$('row-modify-course-submit').className = 'invisible';"
				class="morelink"><b>Add New Course</b></a>
			</td></tr>
		</thead>
		<thead>
			<tr>
			<th>Title</th>
			<th>Description</th>
			<th>Date Created</th>
			<th>Term</th>
			<th>Action</th>
			</tr>
		</thead>
		<tbody>
			<?php
			$courses = $instructor->getAllCourses();
			if($courses != null) {
			$count = 0;
			foreach($courses as $c) {
			//$groups = $c->getAllGroups();
				print "<tr>
				<td><a href='./course.php?c=".$c->id."'>".$c->title."</a></td>
				<td>".$c->description."</td>
				<td>".$c->dd."</td>
				<td>".$c->term."</td>
				<td>
				<a href='javascript:void(0);' onclick='javascript:return sureDel(\"Course\",\"$c->title\",\"./myetat.php?dc=".$c->id."\");'  >[Delete]</a>
				<a href='javascript:modify_course_popup(&quot;$c->id&quot;)'>[Modify]</a></td>
				</tr>";
				$count++;
				}
			}
			?>
		</tbody>
	</table>
</div>
</div>
<?php require_once 'footer.php'; ?>